package com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.GridLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekBannerModel
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekClassifyMpdel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_rc_classify.*
import kotlinx.android.synthetic.main.activity_rc_seek.*
import kotlinx.android.synthetic.main.re_rc_seek_classify.*
import kotlinx.android.synthetic.main.re_youthpost_home_more.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 搜索页实现
 */
class RcSeekActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_rc_seek)
        title = "搜索"

        // 设置轮播图
        setBanner()

        // 垃圾分类
        setClassify()

        // 输入搜索实现
        setEditText()
    }

    /**
     * 输入搜索实现
     */
    private fun setEditText() {
        rcSeekEditText.setOnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                // 判断是否为空！
                if (rcSeekEditText.text.toString().isNotEmpty()){
                    startActivity(
                        Intent(this, SeekShowListActivity::class.java).putExtra(
                            "content",
                            rcSeekEditText.text.toString()
                        )
                    )
                }else "你还没有输入内容呢！".show()

            }
            false
        }
    }

    /**
     * 配置垃圾分类
     */
    private fun setClassify() {
        // 开启协程发起网络请求
        CoroutineScope(Dispatchers.IO).launch {
            // 获取垃圾分类
            api.getRcSeekClassify(getToken()).apply {
                // 判断是否获取到数据
                if (this.code == 200) {
                    val rows = this.rows
                    runOnUiThread {
                        // 填充数据到列表
                        rcSeekRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = GridLayoutManager(context, 4)
                            this.adapter =
                                SeekListRvAdapter(ArrayList<RcSeekClassifyMpdel.RowsBean>().apply {
                                    this.addAll(rows!!)
                                }, R.layout.re_rc_seek_classify)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }
    }

    /**
     * 设置轮播图
     */
    private fun setBanner() {
        // 开启协程发起网络请求
        CoroutineScope(Dispatchers.IO).launch {
            // 获取网络轮播图
            api.getRcSeekBanner(getToken()).apply {
                // 判断是否获取到数据
                if (this.code == 200) {
                    runOnUiThread {
                        val data = this.data
                        rcSeekBanner.apply {
                            this.addBannerLifecycleObserver(this@RcSeekActivity).indicator =
                                CircleIndicator(context)
                            this.setBannerRound(25f)
                            this.adapter =
                                object : BannerImageAdapter<RcSeekBannerModel.DataBean>(data) {
                                    override fun onBindView(
                                        p0: BannerImageHolder?,
                                        p1: RcSeekBannerModel.DataBean?,
                                        p2: Int,
                                        p3: Int
                                    ) {
                                        // 网络图片加载
                                        glide(url + p1?.imgUrl, p0?.imageView!!)
                                    }
                                }
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }
    }

    /**
     * 垃圾分类列表适配器
     */
    inner class SeekListRvAdapter(
        val list: MutableList<RcSeekClassifyMpdel.RowsBean>,
        layoutId: Int
    ) : BaseRecyclerAdapter<RcSeekClassifyMpdel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: RcSeekClassifyMpdel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            glide(url + d.imgUrl, fv.rcSeekImage)
            fv.rcSeekTitle.text = d.name

            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, RcShowClassifyActivity::class.java).putExtra(
                    "content",
                    d.name.toString()
                ))
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}